// 1.先引入样式重置表
require('../../assets/css/index.js');

// 引入字体图标
require('../../assets/iconfonts/iconfont.css')


//2.引入当前页面样式
require('./userInfo.less');


// 3.辅助类功具函数
let dom = require('../../utils/dom');
let http = require('../../utils/http');
let local = require('../../utils/local')

http.getygc()
// 4.当前页面的js文件
dom.ready(function () {
    // 显示底部菜单
    backBtn.addEventListener('click', function () {
        window.history.go(-1)
    })

   
    dom.getEl('#UserPic').addEventListener('click', function () {
        dom.getEl('#foundfile').click();
    })
    let UserPicurl;
    dom.getEl('#foundfile').addEventListener('change', function () {

        let fileval = this.files[0]
        http.uploadpic('file', fileval, '/api/shared/uploadPortrait1', 'ygc', function (res) {
            if (res.errno == 0) {
                dom.getEl('#UserPic').src = http.baseURL + res.data.url
                UserPicurl= res.data.url
            }
        })
    })
    dom.getEl('#Sex').addEventListener('click', function () {
        weui.picker(
            [
                {
                    label: '男',
                    value: '男'
                },
                {
                    label: '女',
                    value: '女'
                }
            ], {
                onConfirm: function (res) {
                    dom.getEl('#Sexval').innerText = res[0].value
                },
                title: '选择性别'
            }
        )

    })
    // 生日
    dom.getEl('#Bir').addEventListener('click', function () {
        weui.datePicker(
            {
                start: 1900,
                end: new Date().getFullYear(),
                onConfirm: function (day) {
                    let Time = day.map(function (v) {
                        return http.Zero(v.value);
                    }).join('-');
                    dom.getEl('#Birsday').innerText = Time
                },
                title: '生日'
            }
        )
    })

    // 省份
    let province;
    dom.getEl('#province').addEventListener('click', function () {
        http.get('/api/shared/province', {}, function (res) {
            if (res.errno == 0) {
                // console.log(res);

                let city = res.data.map(function (v) {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })

                weui.picker(city, {
                    onConfirm: function (res) {
                        dom.getEl('#provinceText').innerText = res[0].label
                        province=res[0]
                    },
                    title: '选择省份'
                }
                )
            }

        })
    })

    // 城市
    let citys;
    dom.getEl('#City').addEventListener('click', function () {
        if(province){

            http.get('/api/shared/city',{provinceId:province.value},function(res){
                let CityNo=res.data.map(function(s){
                    return {
                        label:s.name,
                        value:s.id
                    }
                })
                weui.picker(CityNo, {
                    onConfirm: function (res) {
                        dom.getEl('#CityText').innerText = res[0].label

                        citys=res[0].value
                    },
                    title: '选择省份'
                }
                )
                
            })
        }
    })

    // 输入框
    dom.getEl('#inps').addEventListener('input',function(){
        this.value=this.value.slice(0,100)
        dom.getEl('#numText').innerText = this.value.length
    })

    // 保存
    dom.getEl('#SaveBtn').addEventListener('click',function(){
        let datas = {
            nickName:dom.getEl('#UserName').value,
            gender:dom.getEl('#Sexval').innerText,
            provinceId:province,
            cityId:citys,
            birthday:dom.getEl('#Birsday').innerText,
            imgUrl:UserPicurl,
            sign:inps.value
        }
        http.post('/api/user/changeInfo',datas,function(res){
            if(res.errno==0){
                window.location.href = '/my.html'
                
            }
        })
    })

    // 回填数据
    function BackText(){
        let data =JSON.parse(local.get('userInfo')) 
        console.log(data);
        
        UserPic.src=http.baseURL+data.imgUrl
        UserPicurl=data.imgUrl;

        UserName.value = data.nickName

        Sexval.innerText = data.gender?data.gender:'女'

        Birsday.innerText = data.birthday

        inps.value=data.sign
    }
    BackText()
})